package com.haierCamera.customapplication.ui.plan;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;

import com.haierCamera.customapplication.BaseActivity;
import com.haierCamera.customapplication.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by AdministratorZJ
 * on 2018/6/23.
 */

public class HZKLPeriodSettingActivity extends BaseActivity {
    private ListView listView;
    private String[] period = {"每周一", "每周二", "每周三", "每周四", "每周五", "每周六", "每周日", "每天"};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hzkl_activity_period_time);
        initTitle();
        initData();
    }

    String week = "";
    int mPosition;
    List<Integer> check = new ArrayList<>();

    private void initData() {
        listView = (ListView) findViewById(R.id.period_listView);
        MyAdapter myAdapter = new MyAdapter();
        listView.setAdapter(myAdapter);
        listView.setOnItemClickListener((parent, view, position, id) -> {
            boolean isSelect = false;
            for (int i = 0; i < check.size(); i++) {
                if (position== check.get(i)) {
                    isSelect = true;
                    check.remove(i);
                }
            }
            if (!isSelect) {
                if (position == period.length - 1) {
                    check.clear();
                } else {
                    for (int i = 0; i < check.size(); i++) {
                        if (check.get(i) == period.length -1) {
                            check.remove(i);
                        }
                    }
                }
                check.add(position);
            }
//            if (position == period.length - 1) {
//                check.clear();
//            } else {
//                if (check.contains(period.length - 1)) {
//                    check.remove(0);
//                }
//            }

            myAdapter.notifyDataSetChanged();
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.hzkl_sure, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
            case R.id.hzkl_sure:
                if (check .size() ==7) {
                    week = "每天";
                } else {
                    for (int i = 0; i < check.size(); i++) {
                        week = week + period[check.get(i)];
                    }
                }
                Intent intent = new Intent();
                intent.putExtra("period", week);
                setResult(RESULT_OK, intent);
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 初始化标题栏
     */
    public void initTitle() {
        // 绘制标题
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }


    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return period.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = View.inflate(HZKLPeriodSettingActivity.this, R.layout.hzkl_activity_period_item, null);
                viewHolder = new ViewHolder();
                viewHolder.time = (TextView) convertView.findViewById(R.id.period_time);
                viewHolder.imageView = (ImageView) convertView.findViewById(R.id.checkbox);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            if (check.contains(position)) {
                viewHolder.imageView.setVisibility(View.VISIBLE);
            } else {
                viewHolder.imageView.setVisibility(View.GONE);
            }
            viewHolder.time.setText(period[position]);
            return convertView;
        }
    }

    class ViewHolder {
        TextView time;
        ImageView imageView;
    }
}
